<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet"  th:href="@{css/oksub.css}">
    <script type="text/javascript"  th:src="@{lib/loading/okLoading.js}"></script>
</head>
<body class="childrenBody seting-pass">
<form class="layui-form changePwd">
    <input id="adminId" type="hidden" th:value="${session.nowUser.uid}">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-block">
            <input type="text" value="admin" disabled class="layui-input layui-disabled" th:value="${session.nowUser.username}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">旧密码</label>
        <div class="layui-input-block">
            <input type="password" value="" placeholder="请输入旧密码" lay-verify="required|oldPwd" class="layui-input pwd">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新密码</label>
        <div class="layui-input-block">
            <input type="password" value="" placeholder="请输入新密码" lay-verify="required|pass" id="oldPwd"
                   class="layui-input pwd">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">确认密码</label>
        <div class="layui-input-block">
            <input type="password" value="" placeholder="请确认密码" lay-verify="required|confirmPwd" class="layui-input pwd">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="changePwd" id="changeThis">立即修改</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript"  th:src="@{lib/layui/layui.js}"></script>
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery,
            $form = $('form');
        okLoading.close();
        //添加验证规则verify
        var user = new Object();

        $.ajax({
            cache: true,
            type: "POST",
            url:"selectOne?uid="+$("#adminId").val(),
            async: false,
            error: function(request) {
                alert("失败!");
            },
            success: function(info) {
                user = info;
            }
        });
        form.verify({
            pass: [
                /^[\S]{6,16}$/
                , '密码必须6到16位，且不能出现空格'
            ],
            oldPwd: function (value, item) {
                if (value != user.password) {
                    return "密码错误，请重新输入！";
                }
            },
            confirmPwd: function (value, item) {
                if (!new RegExp($("#oldPwd").val()).test(value)) {
                    return "两次输入密码不一致，请重新输入！";
                }else{
                    user.password = $("#oldPwd").val();
                }

            },

        });

        //修改密码
        form.on("submit(changePwd)", function (data) {
             var index = layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
            $.post("/userChangePasswordByUid",{"uid":user.uid,"password":user.password},function (data) {
                setTimeout(function () {
                    layer.close(index);
                    layer.msg("密码修改成功,2秒之后将跳转到登录页面！");
                    $(".pwd").val('');
                    setTimeout(function () {
                    top.window.location.href="/login";
                    }, 2000);
                }, 2000);
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        })

    });


</script>
</body>
</html>
